<!--
 * 系统首页
 *
 * @Author: ShanZhu
 * @Date: 2023-12-03
-->
<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item><i
                    class="iconfont icon-home-menu"
                    style="margin: 5px; font-size: 22px"
                >
                    首页</i></el-breadcrumb-item>
        </el-breadcrumb>
        
        <!-- 公告列表 -->
        <NoticeList v-if="isCommonUser && userInfoLoaded" ref="noticeList"></NoticeList>
        
        <el-row style="background-color: white;padding: 0 0 50px 25px;">
            <el-col :span="24" style="font-size: 24px; text-align: center;color: #3eb628">
                <h1>欢迎访问停车场管理系统</h1>
            </el-col>

            <el-col :span="18" style="text-align: center;">
                <img :src="require('@/assets/img/03.jpg')" style="text-align:center;height: 100vh;width: 85vw">
            </el-col>
        </el-row>
    </div>
</template>
<script>
import NoticeList from './person/NoticeList.vue';
import axios from "axios";

export default {
    components: {
        NoticeList
    },
    data() {
        return {
            isCommonUser: false, // 默认不显示公告
            userInfoLoaded: false // 用户信息是否已加载完成
        };
    },
    methods: {
        // 检查是否为普通用户
        checkUserType() {
            try {
                const userInfoStr = window.sessionStorage.getItem("userInfo");
                if (userInfoStr) {
                    const userInfo = JSON.parse(userInfoStr);
                    // 管理员用户名为admin时不显示公告
                    this.isCommonUser = userInfo.username !== 'admin';
                    this.userInfoLoaded = true;
                } else {
                    // 如果没有用户信息，尝试从服务器获取
                    const uid = window.sessionStorage.getItem("token");
                    if (uid) {
                        axios.get("/api/user/getUserByUid?uid=" + uid).then((res) => {
                            // 保存用户信息到 sessionStorage
                            window.sessionStorage.setItem("userInfo", JSON.stringify(res.data.data));
                            // 管理员用户名为admin时不显示公告
                            this.isCommonUser = res.data.data.username !== 'admin';
                            this.userInfoLoaded = true;
                        }).catch((error) => {
                            console.error("获取用户信息失败", error);
                            // 出错时默认认为是普通用户
                            this.isCommonUser = true;
                            this.userInfoLoaded = true;
                        });
                    } else {
                        // 如果没有uid，认为是普通用户
                        this.isCommonUser = true;
                        this.userInfoLoaded = true;
                    }
                }
            } catch (e) {
                console.error("解析用户信息失败", e);
                // 出错时默认认为是普通用户
                this.isCommonUser = true;
                this.userInfoLoaded = true;
            }
        }
    },
    mounted() {
        this.checkUserType();
    },
};
</script>
<style scoped>

</style>